package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.FinanceFind;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.widget.MultiLinesViewNew;
import com.isunland.managesystem.widget.SingleLineViewNew;

/**
 * 对象功能:
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: HuXiaoDan
 * 创建时间: 2017/4/1 11:28
 */
public class FinanceFindDetailFragment extends BaseFragment {
  protected static final String EXTRA_CONTENT =
      "com.isunland.managesystem.ui.FinanceFindDetailFragment.EXTRA_CONTENT";
  @BindView(R.id.tv_discoverIntroduction) SingleLineViewNew tvDiscoverIntroduction;
  @BindView(R.id.tv_title1) SingleLineViewNew tvTitle1;
  @BindView(R.id.wv_context) WebView wvContext;
  @BindView(R.id.tv_remark) MultiLinesViewNew tvRemark;
  @BindView(R.id.tv_filePath) SingleLineViewNew tvFilePath;
  @BindView(R.id.tv_regStaffName) SingleLineViewNew tvRegStaffName;
  @BindView(R.id.tv_regDate) SingleLineViewNew tvRegDate;
  @BindView(R.id.tv_checkStaffName) SingleLineViewNew tvCheckStaffName;
  @BindView(R.id.tv_checkDate) SingleLineViewNew tvCheckDate;

  private FinanceFind mContent;

  @Override public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    setTitleCustom(R.string.findDetail);
    mContent = (FinanceFind) getArguments().getSerializable(EXTRA_CONTENT);
  }

  @Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_finance_find_detail, container, false);
    ButterKnife.bind(this, view);
    bindData();
    return view;
  }

  public void bindData() {

    tvDiscoverIntroduction.setTextContent(mContent.getDiscoverIntroduction());
    tvTitle1.setTextContent(mContent.getTitle());
    tvRemark.setTextContent(mContent.getRemark());
    tvRegStaffName.setTextContent(mContent.getRegStaffName());
    tvRegDate.setTextContent(mContent.getRegDate());
    tvCheckStaffName.setTextContent(mContent.getCheckStaffName());
    tvCheckDate.setTextContent(mContent.getCheckDate());
    tvFilePath.getTvContent().setTextColor(getResources().getColor(R.color.primary));
    tvFilePath.setTextContent(mContent.getFilename());
    tvFilePath.getTvContent().setFocusable(false);
    tvFilePath.getTvContent().setClickable(true);
    tvFilePath.setOnClickContentListener(new View.OnClickListener() {
      @Override public void onClick(View view) {
        downFile(mContent.getFilepath());
      }
    });

    String content = MyUtils.parseWebImage(mContent.getContext());//图片自适应
    wvContext.loadDataWithBaseURL(ApiConst.getUrlMain(), content, "text/html", "utf-8", null);//图文混排
  }

  public static FinanceFindDetailFragment newInstance(FinanceFind content) {

    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CONTENT, content);
    FinanceFindDetailFragment fragment = new FinanceFindDetailFragment();
    fragment.setArguments(args);
    return fragment;
  }

  private void downFile(String serverPath) {
    if (!TextUtils.isEmpty(serverPath)) {
      ExtraDownLoadDialogFragment dialog = ExtraDownLoadDialogFragment.newInstance("", serverPath);
      FragmentManager fm = getActivity().getSupportFragmentManager();
      dialog.show(fm, "");
    }
  }
}
